package com.hutao.medical.controller.statics;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.http.client.HttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hutao.medical.common.config.ConfigProperties;
import com.hutao.medical.common.util.ExcelUtil;
import com.hutao.medical.common.util.HttpClientUtil;
import com.hutao.medical.common.util.JsonUtils;
import com.hutao.medical.common.util.Params;
import com.hutao.medical.common.util.Result;
import com.hutao.medical.service.ImportExcelService;

@Controller
@RequestMapping("exportExcel")
public class ExportExcelController {
	
	@Autowired
	private ImportExcelService importExcelService;
	
	@Autowired
	private ConfigProperties configProperties;

	//诊断疾病顺位--主要诊断
	@RequestMapping("/exportDiagnoseDisease")
	public void exportDiagnoseDisease(String beginOutHospitalDate, String endOutHospitalDate, String outDeptName, 
			HttpServletResponse response) {
		Map<String, String> params = new HashMap<>();
		
		try {
			params.put("beginOutHospitalDate", beginOutHospitalDate);
			params.put("endOutHospitalDate", endOutHospitalDate);
			params.put("outDeptName", outDeptName);
			
			String jsonStr = HttpClientUtil.doPost(configProperties.getMedicalManage().getAjax_getMainDiagnoseDiseaseTop12(), params);
			Result result = JsonUtils.jsonToPojo(jsonStr, Result.class);
			
			if (result.isSuccess()) {
					List<Map<String, Object>> list = (List<Map<String, Object>>) result.getData();
					
					
					String excelName = "诊治疾病顺位报表";  
					//转码防止乱码  
					response.addHeader("Content-Disposition", "attachment;filename="+new String(excelName.getBytes("gb2312"), "ISO8859-1" )+".xls");  
					//通过response获取文件输出流
					OutputStream out = response.getOutputStream();
					
					String title="诊治疾病顺位";
					String[] header={"编码","疾病名称", "数量", "构成比"};
					ExcelUtil.exportExcel(out, title, header, list);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	//诊断疾病顺位--次要诊断
	@RequestMapping("/exportSecondDiagnoseDisease")
	public void exportSecondDiagnoseDisease(String beginOutHospitalDate, String endOutHospitalDate, String outDeptName, 
			HttpServletResponse response) {
		Map<String, String> params = new HashMap<>();
		
		try {
			params.put("beginOutHospitalDate", beginOutHospitalDate);
			params.put("endOutHospitalDate", endOutHospitalDate);
			params.put("outDeptName", outDeptName);
			
			String jsonStr = HttpClientUtil.doPost(configProperties.getMedicalManage().getAjax_getSecondDiagnoseDiseaseTop12(), params);
			Result result = JsonUtils.jsonToPojo(jsonStr, Result.class);
			
			if (result.isSuccess()) {
				List<Map<String, Object>> list = (List<Map<String, Object>>) result.getData();
				
					String excelName = "诊治疾病顺位报表";  
					//转码防止乱码  
					response.addHeader("Content-Disposition", "attachment;filename="+new String(excelName.getBytes("gb2312"), "ISO8859-1" )+".xls");  
					//通过response获取文件输出流
					OutputStream out = response.getOutputStream();
					
					String title="诊治疾病顺位";
					String[] header={"编码","疾病名称", "数量", "构成比"};
					ExcelUtil.exportExcel(out, title, header, list);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	//医院手术顺位
	@RequestMapping("/exportMainOperationTop")
	public void exportMainOperationTop(String beginOutHospitalDate, String endOutHospitalDate, String outDeptName, 
			HttpServletResponse response) {
		Map<String, String> params = new HashMap<>();
		
		try {
			params.put("beginOutHospitalDate", beginOutHospitalDate);
			params.put("endOutHospitalDate", endOutHospitalDate);
			params.put("outDeptName", outDeptName);
			
			String jsonStr = HttpClientUtil.doPost(configProperties.getMedicalManage().getAjax_getMainOperationTop(), params);
			Result result = JsonUtils.jsonToPojo(jsonStr, Result.class);
			
			if (result.isSuccess()) {
				List<Map<String, Object>> list = (List<Map<String, Object>>) result.getData();
				
					String excelName = "住院手术顺位报表";  
					//转码防止乱码  
					response.addHeader("Content-Disposition", "attachment;filename="+new String(excelName.getBytes("gb2312"), "ISO8859-1" )+".xls");  
					//通过response获取文件输出流
					OutputStream out = response.getOutputStream();
					
					String title="住院手术顺位";
					String[] header={"编码","手术名称", "数量", "构成比"};
					ExcelUtil.exportExcel(out, title, header, list);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	@RequestMapping("/exportMedicalToExcel")
	public void exportExcel(Params params,HttpServletResponse response){
		List<Map<String,Object>> scanWorkMapList=new ArrayList<>();
		
		try {
			response.setContentType("octets/stream");
	        String excelName = "病案上架报表";  
			//转码防止乱码  
			response.addHeader("Content-Disposition", "attachment;filename="+new String(excelName.getBytes("gb2312"), "ISO8859-1" )+".xls");  
			//通过response获取文件输出流
			OutputStream out = response.getOutputStream();
		
			//查询数据
			scanWorkMapList=this.generateDataMedical();
			System.out.println(scanWorkMapList.size());
			String[] bases={"String","String","String"};
			String title="病案上架";
			String[] header={"病房","出院病案数"	,"上架病案数"};
			ExcelUtil.exportExcel(out, title, bases, header, scanWorkMapList);
			
		} catch (Exception e) {
			e.printStackTrace();
			
		}
	}
	
	public List<Map<String, Object>> generateDataMedical(){
		List<Map<String,Object>> list=new ArrayList<>();
		Map<String,Object> map1=new  LinkedHashMap<>();
		map1.put("inpatientWard","101011-呼吸内科病房");
		map1.put("DischargeNum ","196");
		map1.put("groundingNum","195");
		list.add(map1);
		Map<String,Object> map2=new  LinkedHashMap<>();
		map2.put("inpatientWard","101021-消化一科病房");
		map2.put("DischargeNum ","156");
		map2.put("groundingNum","156");
		list.add(map2);
		Map<String,Object> map3=new  LinkedHashMap<>();
		map3.put("inpatientWard","101022-消化二科病房");
		map3.put("DischargeNum ","178");
		map3.put("groundingNum","178");
		list.add(map3);
		Map<String,Object> map4=new  LinkedHashMap<>();
		map4.put("inpatientWard","101031-感染性疾病病房");
		map4.put("DischargeNum ","112");
		map4.put("groundingNum","111");
		list.add(map4);
		Map<String,Object> map5=new  LinkedHashMap<>();
		map5.put("inpatientWard","101041-心内科病房");
		map5.put("DischargeNum ","64");
		map5.put("groundingNum","64");
		list.add(map5);
		return list;
	}
	
	@RequestMapping("/exportPrintToExcel")
	public void exportPrintToExcel(Params params,HttpServletResponse response){
		List<Map<String,Object>> scanWorkMapList=new ArrayList<>();
		
		try {
			response.setContentType("octets/stream");
	        String excelName = "打印质控报表";  
			//转码防止乱码  
			response.addHeader("Content-Disposition", "attachment;filename="+new String(excelName.getBytes("gb2312"), "ISO8859-1" )+".xls");  
			//通过response获取文件输出流
			OutputStream out = response.getOutputStream();
		
			//查询数据1
			scanWorkMapList=this.generateDataPrint();
			String[] bases={"String","String","String"};
			String title="打印质控";
			String[] header={"操作者","打印病案数"	,"打印页数"};
			ExcelUtil.exportExcel(out, title, bases, header, scanWorkMapList);
			
		} catch (Exception e) {
			e.printStackTrace();
			
		}
	}
	
	public List<Map<String, Object>> generateDataPrint(){
		List<Map<String,Object>> list=new ArrayList<>();
		Map<String,Object> map1=new  LinkedHashMap<>();
		map1.put("operator", "李爱玲");
		map1.put("printMedicalnum", "102");
		map1.put("printPagenum", "8402");
		list.add(map1);
		Map<String,Object> map2=new  LinkedHashMap<>();
		map2.put("operator", "张霄");
		map2.put("printMedicalnum", "101");
		map2.put("printPagenum", "4302");
		list.add(map2);
		Map<String,Object> map3=new  LinkedHashMap<>();
		map3.put("operator", "张瑶");
		map3.put("printMedicalnum", "43");
		map3.put("printPagenum", "1210");
		list.add(map3);
		Map<String,Object> map4=new  LinkedHashMap<>();
		map4.put("operator", "陈菲");
		map4.put("printMedicalnum", "79");
		map4.put("printPagenum", "2100");
		list.add(map4);
		return list;
	}
	
	@RequestMapping("/exportScanToExcel")
	public void exportScanToExcel(Params params,HttpServletResponse response){
		List<Map<String,Object>> scanWorkMapList=new ArrayList<>();
		
		try {
			response.setContentType("octets/stream");
	        String excelName = "扫描质控报表";  
			//转码防止乱码  
			response.addHeader("Content-Disposition", "attachment;filename="+new String(excelName.getBytes("gb2312"), "ISO8859-1" )+".xls");  
			//通过response获取文件输出流
			OutputStream out = response.getOutputStream();
		
			//查询数据
			scanWorkMapList=this.generateDataScan();
			String[] bases={"String","String","String","String","String"};
			String title="扫描质控";
			String[] header={"操作者","打印病案数"	,"打印页数","编制页面","未编制页面","编制出错率","编制出错率"};
			ExcelUtil.exportExcel(out, title, bases, header, scanWorkMapList);
			
		} catch (Exception e) {
			e.printStackTrace();
			
		}
	}
	public List<Map<String, Object>> generateDataScan(){
		List<Map<String,Object>> list=new ArrayList<>();
		Map<String,Object> map1=new  LinkedHashMap<>();
		map1.put("operator", "李爱玲");
		map1.put("printMedicalnum", "102");
		map1.put("printPagenum", "8402");
		map1.put("organizationPagenum", "2400");
		map1.put("notorganizationPagenum", "6002");
		list.add(map1);
		Map<String,Object> map2=new  LinkedHashMap<>();
		map2.put("operator", "张霄");
		map2.put("printMedicalnum", "101");
		map2.put("printPagenum", "4302");
		map2.put("organizationPagenum", "2300");
		map2.put("notorganizationPagenum", "2002");
		list.add(map2);
		Map<String,Object> map3=new  LinkedHashMap<>();
		map3.put("operator", "张瑶");
		map3.put("printMedicalnum", "43");
		map3.put("printPagenum", "1210");
		map3.put("organizationPagenum", "1210");
		map3.put("notorganizationPagenum", "1210");
		list.add(map3);
		Map<String,Object> map4=new  LinkedHashMap<>();
		map4.put("operator", "陈菲");
		map4.put("printMedicalnum", "79");
		map4.put("printPagenum", "2100");
		map4.put("organizationPagenum", "1900");
		map4.put("notorganizationPagenum", "200");
		list.add(map4);
		return list;
	}
	
	@RequestMapping("exportCountToExcel")
	public void exportCountToExcel(Params params,HttpServletResponse response){
		List<Map<String,Object>> scanWorkMapList=new ArrayList<>();
		
		try {
			response.setContentType("octets/stream");
	        String excelName = "病房工作量动态日报表";  
			//转码防止乱码  
			response.addHeader("Content-Disposition", "attachment;filename="+new String(excelName.getBytes("gb2312"), "ISO8859-1" )+".xls");  
			//通过response获取文件输出流
			OutputStream out = response.getOutputStream();
		
			//查询数据
			scanWorkMapList=this.generateDataCount();
			String[] bases={"String","String","String","String","String","String","String","String"};
			String title="病房工作量动态日报表";
			String[] header={"科室","实有床位数"	,"原有病人数","入院病人数","他科转入人数","转往他科人数","出院病人数","实有病人数"};
			ExcelUtil.exportExcel(out, title, bases, header, scanWorkMapList);
			
		} catch (Exception e) {
			e.printStackTrace();
			
		}
	}
	
	public List<Map<String, Object>> generateDataCount(){
		List<Map<String,Object>> list=new ArrayList<>();
		Map<String,Object> map1=new  LinkedHashMap<>();
		map1.put("department", "101011-呼吸内科病房");
		map1.put("actualBedNum", "50");
		map1.put("originalPatientNum", "41");
		map1.put("inhospitalPatientNum", "1");
		map1.put("OthershiftPeopleNum ", "1");
		map1.put("shiftOtherPeopleNum", "0");
		map1.put("outhospitalPatientNum", "1");
		map1.put("actualPatientNum", "42");
		list.add(map1);
		Map<String,Object> map2=new  LinkedHashMap<>();
		map2.put("department", "101021-消化一科病房");
		map2.put("actualBedNum", "55");
		map2.put("originalPatientNum", "35");
		map2.put("inhospitalPatientNum", "0");
		map2.put("OthershiftPeopleNum ", "0");
		map2.put("shiftOtherPeopleNum", "0");
		map2.put("outhospitalPatientNum", "1");
		map2.put("actualPatientNum", "34");
		list.add(map2);
		Map<String,Object> map3=new  LinkedHashMap<>();
		map3.put("department", "101022-外科病房");
		map3.put("actualBedNum", "60");
		map3.put("originalPatientNum", "56");
		map3.put("inhospitalPatientNum", "1");
		map3.put("OthershiftPeopleNum ", "1");
		map3.put("shiftOtherPeopleNum", "0");
		map3.put("outhospitalPatientNum", "1");
		map3.put("actualPatientNum", "57");
		list.add(map3);
		Map<String,Object> map4=new  LinkedHashMap<>();
		map4.put("department", "101031-感染性疾病病房");
		map4.put("actualBedNum", "50");
		map4.put("originalPatientNum", "41");
		map4.put("inhospitalPatientNum", "1");
		map4.put("OthershiftPeopleNum ", "1");
		map4.put("shiftOtherPeopleNum", "0");
		map4.put("outhospitalPatientNum", "1");
		map4.put("actualPatientNum", "42");
		list.add(map4);
		Map<String,Object> map5=new  LinkedHashMap<>();
		map5.put("department", "101041-心内科病房");
		map5.put("actualBedNum", "75");
		map5.put("originalPatientNum", "49");
		map5.put("inhospitalPatientNum", "1");
		map5.put("OthershiftPeopleNum ", "1");
		map5.put("shiftOtherPeopleNum", "0");
		map5.put("outhospitalPatientNum", "2");
		map5.put("actualPatientNum", "49");
		list.add(map5);
		return list;
	}
	
	@RequestMapping("exportOutputToExcel")
	public void exportOutputToExcel(Params params,HttpServletResponse response){
		List<Map<String,Object>> scanWorkMapList=new ArrayList<>();
		
		try {
			response.setContentType("octets/stream");
	        String excelName = "输出报表";  
			//转码防止乱码  
			response.addHeader("Content-Disposition", "attachment;filename="+new String(excelName.getBytes("gb2312"), "ISO8859-1" )+".xls");  
			//通过response获取文件输出流
			OutputStream out = response.getOutputStream();
		
			//查询数据
			scanWorkMapList=this.generateDataOutput();
			String[] bases={"String","String","String","String","String","String","String","String","String","String","String","String","String","String","String","String"};
			String title="输出报表";
			String[] header={"日期","科室","实开床位数","实有床位数"	,"原有病人数","入院病人数","他科转入人数","转往他科人数","出院病人数","实有病人数","病危人数","抢救人数","抢救成功人数","分娩人数","手术人数","特级护理"};
			ExcelUtil.exportExcel(out, title, bases, header, scanWorkMapList);
			
		} catch (Exception e) {
			e.printStackTrace();
			
		}
	}
	
	public List<Map<String, Object>> generateDataOutput(){
		List<Map<String,Object>> list=new ArrayList<>();
		Map<String,Object> map1=new  LinkedHashMap<>();
		map1.put("date", "2017-10-1");
		map1.put("department", "101011-呼吸内科病房");
		map1.put("actualBedNum", "48");
		map1.put("actualOpenBedNum", "50");
		map1.put("originalPatientNum", "48");
		map1.put("inhospitalPatientNum", "40");
		map1.put("OthershiftPeopleNum ", "1");
		map1.put("shiftOtherPeopleNum", "0");
		map1.put("outhospitalPatientNum", "0");
		map1.put("actualPatientNum", "2");
		map1.put("dyingPatientNum", "39");
		map1.put("rescuePatientNum", "0");
		map1.put("rescueSuccessPatientNum", "0");
		map1.put("childbirthPatientNum", "0");
		map1.put("Operation PatientNum", "0");
		map1.put("specialNursing", "1");
		list.add(map1);
		Map<String,Object> map2=new  LinkedHashMap<>();
		map2.put("date", "2017-10-2");
		map2.put("department", "101011-呼吸内科病房");
		map2.put("actualBedNum", "48");
		map2.put("actualOpenBedNum", "50");
		map2.put("originalPatientNum", "48");
		map2.put("inhospitalPatientNum", "40");
		map2.put("OthershiftPeopleNum ", "1");
		map2.put("shiftOtherPeopleNum", "0");
		map2.put("outhospitalPatientNum", "0");
		map2.put("actualPatientNum", "2");
		map2.put("dyingPatientNum", "39");
		map2.put("rescuePatientNum", "0");
		map2.put("rescueSuccessPatientNum", "0");
		map2.put("childbirthPatientNum", "0");
		map2.put("Operation PatientNum", "0");
		map2.put("specialNursing", "1");
		list.add(map2);
		Map<String,Object> map3=new  LinkedHashMap<>();
		map3.put("date", "2017-10-3");
		map3.put("department", "101011-呼吸内科病房");
		map3.put("actualBedNum", "48");
		map3.put("actualOpenBedNum", "50");
		map3.put("originalPatientNum", "48");
		map3.put("inhospitalPatientNum", "40");
		map3.put("OthershiftPeopleNum ", "1");
		map3.put("shiftOtherPeopleNum", "0");
		map3.put("outhospitalPatientNum", "0");
		map3.put("actualPatientNum", "2");
		map3.put("dyingPatientNum", "39");
		map3.put("rescuePatientNum", "0");
		map3.put("rescueSuccessPatientNum", "0");
		map3.put("childbirthPatientNum", "0");
		map3.put("Operation PatientNum", "0");
		map3.put("specialNursing", "1");
		list.add(map3);
		Map<String,Object> map4=new  LinkedHashMap<>();
		map4.put("date", "2017-10-4");
		map4.put("department", "101011-呼吸内科病房");
		map4.put("actualBedNum", "48");
		map4.put("actualOpenBedNum", "50");
		map4.put("originalPatientNum", "48");
		map4.put("inhospitalPatientNum", "40");
		map4.put("OthershiftPeopleNum ", "1");
		map4.put("shiftOtherPeopleNum", "0");
		map4.put("outhospitalPatientNum", "0");
		map4.put("actualPatientNum", "2");
		map4.put("dyingPatientNum", "39");
		map4.put("rescuePatientNum", "0");
		map4.put("rescueSuccessPatientNum", "0");
		map4.put("childbirthPatientNum", "0");
		map4.put("Operation PatientNum", "0");
		map4.put("specialNursing", "1");
		list.add(map4);
		Map<String,Object> map5=new  LinkedHashMap<>();
		map5.put("date", "2017-10-5");
		map5.put("department", "101011-呼吸内科病房");
		map5.put("actualBedNum", "48");
		map5.put("actualOpenBedNum", "50");
		map5.put("originalPatientNum", "48");
		map5.put("inhospitalPatientNum", "40");
		map5.put("OthershiftPeopleNum ", "1");
		map5.put("shiftOtherPeopleNum", "0");
		map5.put("outhospitalPatientNum", "0");
		map5.put("actualPatientNum", "2");
		map5.put("dyingPatientNum", "39");
		map5.put("rescuePatientNum", "0");
		map5.put("rescueSuccessPatientNum", "0");
		map5.put("childbirthPatientNum", "0");
		map5.put("Operation PatientNum", "0");
		map5.put("specialNursing", "1");
		list.add(map5);
		return list;
	}
	
	

	@ResponseBody
	@RequestMapping("/addMedicalExamnation")
	public String addMedicalExamnation(){
		List list=ExcelUtil.readXlsx("E://最新审核规则.xlsx");
		importExcelService.excelImportToDatabase(list);
		System.out.println(list);
		//statiService.addMedRecordExamnation(list);
		return "statics/medQua";
	}
	
	
}
